Imports OMANE.Common.UI
Imports System.Collections.ObjectModel
Imports System.Windows
Imports Infragistics.Windows.Chart
Imports System.Windows.Controls
Imports System.ComponentModel
Imports PTIC_GestionBDD

''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
Public Class VMEditableProperty
    Inherits ViewModelBase

    Private propdesc As PropertyDescriptor

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()

    End Sub
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New(ByVal p As PropertyDescriptor)
        propdesc = p
        _Name = p.Name
        _Type = p.PropertyType
        If _Type.IsEnum Then
            Extras.EnumValues = [Enum].GetValues(_Type)
        End If
    End Sub
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New(ByVal p As PropertyDescriptor, ByVal t As Object)
        propdesc = p
        _Name = p.Name
        _Type = p.PropertyType
        If _Type.IsEnum Then
            Extras.EnumValues = [Enum].GetValues(_Type)
        End If
        _Target = t
        _Value = p.GetValue(_Target)
    End Sub
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New(ByVal name As String, ByVal type As Type)
        Me._Name = name
        Me._Type = type
    End Sub

    Private _Name As String
    Private _Type As Type
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public ReadOnly Property Name As String
        Get
            Return _Name
        End Get
    End Property
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public ReadOnly Property Type As Type
        Get
            Return _Type
        End Get
    End Property

    Private _Target As Object
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property Target As Object
        Get
            Return _Target
        End Get
        Set(ByVal value As Object)
            OnPropertyChanging("Target")
            _Target = value
            OnPropertyChanged("Target")
        End Set
    End Property

    Private _Value As Object
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property Value As Object
        Get
            Return _Value
        End Get
        Set(ByVal value As Object)
            OnPropertyChanging("Value")
            _Value = value
            If propdesc IsNot Nothing Then
                If _Target IsNot Nothing Then
                    'propdesc.Converter.SetValue(_Target, _Value)
                    If value Is Nothing Then
                        propdesc.SetValue(_Target, _Value)
                    Else
                        If Type.IsAssignableFrom(value.GetType()) Then
                            propdesc.SetValue(_Target, _Value)
                        Else
                            If propdesc.Converter IsNot Nothing Then
                                If propdesc.Converter.CanConvertFrom(value.GetType()) Then
                                    propdesc.SetValue(_Target, propdesc.Converter.ConvertFrom(Nothing, System.Globalization.CultureInfo.InvariantCulture, value))
                                End If
                            End If
                        End If
                    End If
                End If
            End If
            OnPropertyChanged("Value")
        End Set
    End Property

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property Extensions As New ObservableCollection(Of VMEditablePropertyExtension)

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property EditorTemplate As Object
End Class

Public Class VMEditableCommand
    Implements Input.ICommand

    Private meth As Reflection.MethodInfo
    Private Target As Object
    Private text As String
    Sub New(ByVal meth As Reflection.MethodInfo, ByVal t As Object, ByVal text As String)
        Me.meth = meth
        Me.Target = t
        Me.text = text

    End Sub

    Public ReadOnly Property Name As String
        Get
            Return If(String.IsNullOrEmpty(text), _
                      meth.DeclaringType.FullName & "." & meth.Name, _
                      text)
        End Get
    End Property

    Public Function CanExecute(ByVal parameter As Object) As Boolean Implements System.Windows.Input.ICommand.CanExecute
        Return True
    End Function

    Public Event CanExecuteChanged(ByVal sender As Object, ByVal e As System.EventArgs) Implements System.Windows.Input.ICommand.CanExecuteChanged

    Public Sub Execute(ByVal parameter As Object) Implements System.Windows.Input.ICommand.Execute
        meth.Invoke(Me.Target, New Object() {Nothing})
    End Sub
End Class

''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
Public Class VMEditablePropertyExtension
    Inherits ViewModelBase


    Private _Name As String
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property Name As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            OnPropertyChanging("Name")
            _Name = value
            OnPropertyChanged("Name")
        End Set
    End Property


    Private _Value As Object
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property Value As Object
        Get
            Return _Value
        End Get
        Set(ByVal value As Object)
            OnPropertyChanging("Value")
            _Value = value
            OnPropertyChanged("Value")
        End Set
    End Property


End Class